package demo2;

import org.junit.Test;

import java.io.*;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.charset.StandardCharsets;

/**
 * @description:实现TCP的网络编程
 * @author:Mr.Cat
 * @create:2021-11-14 15:16
 **/
public class TCPtest1 {

    //客户端
    @Test
    public void client() {
        Socket socket = null;
        OutputStream os = null;
        try {
            //创建socket的对象，指明服务器端的IP和端口号
            InetAddress inet = InetAddress.getByName("127.0.0.1");
            socket = new Socket(inet, 8866);
            //获取一个输出流，用于输出数据
            os = socket.getOutputStream();

            //写出数据
            os.write("你好，我是客户端".getBytes(StandardCharsets.UTF_8));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //服务端
    @Test
    public void server() {
        //创建服务器端的ServerSocket，指明自己的端口号
        ServerSocket ss = null;
        Socket socket = null;
        InputStream is = null;
        ByteArrayOutputStream baos = null;
        //InputStreamReader isr=null;
        try {
            ss = new ServerSocket(8866);
            //调用accept（）方法表示接受来自于客户端的socket
            socket = ss.accept();
            //获取一个输入流
            is = socket.getInputStream();

            //可以运行
//            isr = new InputStreamReader(is);
//            char [] buffer = new char[20];
//            int len;
//            while((len = isr.read(buffer))!=-1){
//            String str = new String(buffer,0,len);
//            System.out.println(str);
//             }

            //不建议这样写，字节流转换汉字时可能会乱码
            /*byte[] buffer = new byte[20];
            int len;
            while ((len=is.read(buffer))!=-1){
                String str = new String(buffer,0,len);
                System.out.println(str);
            }*/

            //读取一个输入流中的数据
            baos = new ByteArrayOutputStream();
            byte[] buffer = new byte[5];
            int len;
            while ((len = is.read(buffer)) != -1) {
                baos.write(buffer, 0, len);
            }
            System.out.println(baos.toString());
            System.out.println(socket.getInetAddress().getHostAddress());
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            //关闭资源
//            if(isr!=null){
//                try{
//                    isr.close();
//                }catch(IOException e){
//                    e.printStackTrace();
//                 }
//            }
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (ss != null) {
                try {
                    ss.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (socket != null) {
                try {
                    socket.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}

